package com.usian.controller;

import com.usian.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
//file/upload
@RestController
@RequestMapping("file")
public class FileController {

    @PostMapping("/upload")
    public Result fileImg(MultipartFile file){
        if (file != null ){
            String imgName = UUID.randomUUID().toString()+".jpg";
            File lu = new File("D:\\img\\"+imgName);
            try {
                file.transferTo(lu);
                return Result.ok("http://image.usian.com/"+imgName);
            } catch (IOException e) {
                e.printStackTrace();
                return Result.error("上传失败2");
            }
        }
        return Result.error("上传失败1");
    }
}
